import mitt from 'mitt'
import { App } from 'vue'
const event = mitt()

export function mountEvent(app: App) {
    app.config.globalProperties.$pub = (eventName: string, params: CommonObject): void => {
        event.emit(eventName, params)
    }

    // 全局订阅（在Vue全局方法中自定义$sub订阅方法）
    app.config.globalProperties.$sub = function (eventName: string, callback: (...args: any[]) => void): void {
        event.on(eventName, callback)
    }

    // 取消订阅
    app.config.globalProperties.$unsub = function (eventName: string, callback: (...args: any[]) => void): void {
        // Reflect.apply(event.off, event, _.toArray(arguments))
        event.on(eventName, callback)
    }
}

export default event
